package teama.servlet;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class testSmtp {
  public static void main( String args[] ) {

    String smtphost = "74.125.127.109";                // SMTPの IP アドレス
    String from     = "s.henzan58@gmail.com";              // 送信元
    String to       = "s.henzan58@gmail.com";      // 送信先
    String subject  = "Test Message";                     // サブジェクト
    String msgtext  = "This is test message.\n ";      // 送信内容

    Properties props = new Properties();
    props.put( "mail.smtp.host", smtphost );
    props.put("mail.host","smtp.gmail.com"); // 接続するホスト名
    props.put( "mail.smtp.port","465"); // ポート番号
    props.put("mail.smtp.auth", "true");

    props.put( "mail.smtp.socketFactory.port", "465");
    props.put( "mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put( "mail.smtp.socketFactory.fallback", String.valueOf( false ) );

    Session session = Session.getDefaultInstance( props, null );
    //Message msg = new MimeMessage( session );
    MimeMessage msg=new MimeMessage(session);
    Transport transport = null;
    try {
    	// 送信元 From
    	InternetAddress fromaddr = new InternetAddress(from, "HENZAN");
    	msg.setFrom( fromaddr );

    	//InternetAddress[] toaddr = { new InternetAddress( to ) };
    	// 送信先 TO
    	msg.setRecipients( Message.RecipientType.TO, to);

    	msg.setSubject( subject );
    	msg.setText( msgtext );

    	transport = session.getTransport("smtp");

    	// ★★★自分のパスワードを入れてください。★★★
    	transport.connect(from,"パスワード");
    	transport.sendMessage(msg, msg.getAllRecipients());
    	Transport.send( msg );

    } catch( AddressException e ) {
      System.out.println( "Address Exception: " + e );

    } catch( MessagingException e ) {
      System.out.println( "Messaging Exception: " + e );
    } catch (Exception e) {
		// TODO: handle exception
	}
  }
}